<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class Users extends Authenticatable
{
    use HasApiTokens;

    protected $table = 'users';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">未通过</span>',
        self::STATUS_4 => '<span class="el-tag el-tag--info">锁定</span>',
    ];

    const SEX_1 = 1;
    const SEX_2 = 2;

    const SEX_TEXT = [
        self::SEX_1 => '<span class="el-tag">男</span>',
        self::SEX_2 => '<span class="el-tag el-tag--danger">女</span>',
    ];

    const TYPE_1 = 1;
    const TYPE_2 = 2;

    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag">人力HR</span>',
        self::TYPE_2 => '<span class="el-tag  el-tag--danger">供应商</span>',
    ];

    protected $fillable = [
        'id','user_sn','username','mobile','nickname','avatar','password','integral','balance','bond','profit','qq','wechat','bank_name',
        'contract','cash','member_fee','member_fee_at','email','openid','sex','qq_id','wechat_id','integral_grade','area','bank_sn',
        'register_ip','login_ip', 'login_at','sign_in_at','status','type','remember_token','created_at','updated_at'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $dates = ['login_at'];

    public function user_info(){//扩展信息
        return $this->belongsTo('App\Models\UserInfos', 'id', 'user_id');
    }
    public function store(){//扩展信息
        return $this->belongsTo('App\Models\Stores', 'id', 'user_id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
            if(isset($params['kwd']) && $params['kwd']) {
                $query ->where('username','like','%' . $params['kwd'] . '%')
//                    ->orWhere('nickname','like','%' . $params['kwd'] . '%')
                ->orWhere('mobile','like','%' . $params['kwd'] . '%');
            }
            if(isset($params['status']) && $params['status']) {
                $query ->where('status',$params['status']);
            }
            if(isset($params['type']) && $params['type']) {
                $query ->where('type',$params['type']);
            }
            if(isset($params['balance']) && $params['balance']) {
                $query ->where('balance','<',0);
            }
            if(isset($params['id']) && $params['id']) {
                $query ->whereNotIn('id',$params['id'])->whereIn('id',$params['ids']);
            }
        })->where('status','<>',-1)
        ->with(['user_info' => function ($query) {
            $query->select(['*'])->get();
        }])->with(['store' => function ($query) {
            $query->select(['*'])->get();
        }])->orderBy('created_at', 'desc')->paginate($limit);
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
            'sex' => self::SEX_TEXT,
            'type' => self::TYPE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function apiList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('username','like','%' . $params['kwd'] . '%')
                        ->orWhere('nickname','like','%' . $params['kwd'] . '%')
                        ->orWhere('mobile','like','%' . $params['kwd'] . '%')
                        ->orWhere('user_sn','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
            })->where('status','<>',-1)
            ->with(['user_info' => function ($query) {
                $query->select(['*'])->get();
            }])->with(['store' => function ($query) {
                $query->select(['*'])->get();
            }])
            ->orderBy('created_at', 'desc')->get();
        return $lists;
    }

    /**
     * 获取用户详细信息
     * @param $data
     * @param array $map
     * @return mixed
     */
    protected function getUserInfo($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['id']) && $params['id']) {
                    $query ->where('id',$params['id']);
                }
                if(isset($params['mobile']) && $params['mobile']) {
                    $query ->where('mobile',$params['mobile']);
                }
                if(isset($params['password']) && $params['password']) {
                    $query ->where('password',$params['password']);
                }
            })->where('status','<>',-1)
            ->with(['user_info' => function ($query) {
                $query->select(['*'])->get();
            }])->first();
        return $lists;
    }

    /**
     * 状态转换
     * @param $data
     * @param array $map
     * @return mixed
     */
    protected function stateToText(&$data, $map = []){
        foreach ($data as $key => &$row) {
            foreach ($map as $col => $pair) {
                if (isset($row[$col]) && isset($pair[$row[$col]])) {
                    $text = $col . '_text';
                    $row[$text] = $pair[$row[$col]];
                }
            }
        }
        return $data;
    }

    /**
     * passport默认使用email字段作为用户名，如果usename是其他字段必须重新定义findForPassport方法，
     * 且必须是public
     * @param $username
     * @return mixed
     */
    public function findForPassport($username)
    {
        return self::where('openid', $username)->first();
    }

    /**
     * 获取HR导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function hrExports($params){
        $fields = [
            'id','nickname','sex','mobile','email','qq','balance','integral','status','login_at'
        ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('type',1)->where('status','<>',-1)->get();
        foreach ($lists as $k=>$v){
            if($v->attributes['sex'] == 1){
                $v->attributes['sex'] = '男';
            }else{
                $v->attributes['sex'] = '女';
            }
            if($v->attributes['status'] == 1){
                $v->attributes['status'] = '正常';
            }elseif($v->attributes['status'] == 2){
                $v->attributes['status'] = '待审核';
            }elseif($v->attributes['status'] == 3){
                $v->attributes['status'] = '未通过';
            }elseif($v->attributes['status'] == 4){
                $v->attributes['status'] = '锁定';
            }
            $v->attributes['company_name'] = UserInfos::where('id',$v['id'])->value('company_name');
            $v->attributes['position'] = UserInfos::where('id',$v['id'])->value('hr_position');
            $company_info = UserInfos::where('id',$v['id'])->first(['company_type','company_industry','company_size']);
            $v->attributes['company_type'] = CompanyInfos::where('id',$company_info['company_type'])->value('name');
            $v->attributes['company_industry'] = CompanyInfos::where('id',$company_info['company_industry'])->value('name');
            $v->attributes['company_size'] = CompanyInfos::where('id',$company_info['company_size'])->value('name');
            unset($v['id']);
        }
        return $lists;
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function supplierExports($params){
        $fields = [
            'id','username','bank_name','bank_sn','sex','mobile','email','qq','wechat','balance','integral','bond',
            'profit','contract','cash','member_fee','member_fee_at','area','status','login_at',
        ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('type',2)->where('status','<>',-1)->get();
        foreach ($lists as $k=>$v){
            if($v->attributes['sex'] == 1){
                $v->attributes['sex'] = '男';
            }else{
                $v->attributes['sex'] = '女';
            }
            if($v->attributes['status'] == 1){
                $v->attributes['status'] = '正常';
            }elseif($v->attributes['status'] == 2){
                $v->attributes['status'] = '待审核';
            }elseif($v->attributes['status'] == 3){
                $v->attributes['status'] = '未通过';
            }elseif($v->attributes['status'] == 4){
                $v->attributes['status'] = '锁定';
            }
            $v->attributes['area'] = Areas::where('id',$v['area'])->value('name');
            $v->attributes['master_name'] = UserInfos::where('id',$v['id'])->value('master_name');
            $v->attributes['qrganization_code'] = UserInfos::where('id',$v['id'])->value('qrganization_code');
            unset($v['id']);
        }
        return $lists;
    }
}
